package spt.tasks;

import java.io.IOException;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

/**
 * 
 * @author Andrju
 * @author Piotrek
 */
public class Weather extends Task {
	private static final Logger log = Logger.getLogger(Weather.class.getName());
	
	private class Day {
		//public Date date;
		public int minimalTemperature;
		public int maximalTemperature;
		public String forecast;
		
		@Override
		public String toString() {
			return "Day [minimalTemperature=" + minimalTemperature
					+ ", maximalTemperature=" + maximalTemperature
					+ ", forecast=" + forecast + "]";
		}
	}
	
	private Day today, tomorrow, afterTomorrow;
	//private Map<String, String> dictionary;
	private int WEATHER_SLIDE_ID = 1;
	
	public Weather(){
		today = null;
		tomorrow = null;
		afterTomorrow = null;
	}
	
	public void execute() {
		//initDictionary();
		try {
			Document document = Jsoup.connect("http://wmo.imgw.pl/wwis/009/c00024.htm").get();

			Element forecastElement = document.getElementsByTag("table").get(14);

			today = new Day();
			tomorrow = new Day();
			afterTomorrow = new Day();

			//today.data = forecastElement.getElementsByTag("td").get(5).text();
			today.minimalTemperature = Integer.parseInt(forecastElement.getElementsByTag("td").get(6).text());
			today.maximalTemperature = Integer.parseInt(forecastElement.getElementsByTag("td").get(7).text());
			today.forecast = forecastElement.getElementsByTag("td").get(9).text();

			//tomorrow.data = forecastElement.getElementsByTag("td").get(11).text();
			tomorrow.minimalTemperature = Integer.parseInt(forecastElement.getElementsByTag("td").get(12).text());
			tomorrow.maximalTemperature = Integer.parseInt(forecastElement.getElementsByTag("td").get(13).text());
			tomorrow.forecast = forecastElement.getElementsByTag("td").get(15).text();

			//afterTomorrow.data = forecastElement.getElementsByTag("td").get(17).text();
			afterTomorrow.minimalTemperature = Integer.parseInt(forecastElement.getElementsByTag("td").get(18).text());
			afterTomorrow.maximalTemperature = Integer.parseInt(forecastElement.getElementsByTag("td").get(19).text());
			afterTomorrow.forecast = forecastElement.getElementsByTag("td").get(21).text();
			
			log.info("Today: " + today + "\nTomorrow: " + tomorrow + "\nAfter tomorrow: " + afterTomorrow);
			
			updateWeatherSlide();
		} catch (IOException ex) {
			log.severe("Could not download forecast from weather provider.");
			log.severe(ex.getMessage());
		}
	}

	private void updateWeatherSlide(){
		StringBuffer slide = new StringBuffer();
		slide.append("<slide display-time=\"20\" always-published=\"true\">");
		slide.append("<weather>");
		slide.append("<day name=\"today\"");
		slide.append(" min-temp=\"" + today.minimalTemperature + "\"");
		slide.append(" max-temp=\"" + today.maximalTemperature + "\"");
		slide.append(" forecast=\"" + today.forecast + "\"");
		slide.append(" />");
		slide.append("<day name=\"tomorrow\"");
		slide.append(" min-temp=\"" + tomorrow.minimalTemperature + "\"");
		slide.append(" max-temp=\"" + tomorrow.maximalTemperature + "\"");
		slide.append(" forecast=\"" + tomorrow.forecast + "\"");
		slide.append(" />");
		slide.append("<day name=\"afterTomorrow\"");
		slide.append(" min-temp=\"" + afterTomorrow.minimalTemperature + "\"");
		slide.append(" max-temp=\"" + afterTomorrow.maximalTemperature + "\"");
		slide.append(" forecast=\"" + afterTomorrow.forecast + "\"");
		slide.append(" />");
		slide.append("</weather>");
		slide.append("</slide>");
		
		//log.info(slide.toString());
		
		//TODO: Update using Jersey Client
		
		Client c = Client.create();
		WebResource webRes = c.resource("http://localhost:8080/SPTServer/backend/slide?id=" + WEATHER_SLIDE_ID);
		webRes.post(String.class, slide.toString());
	}
	
	//private String translateToPL(String ang){
	//	return dictionary.get(ang);
	//}
	
	//private void initDictionary() {
	//	dictionary = new HashMap<String, String>();
	//	//add to dictionary all forecast descriptions
	//}
}
